import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/up-form.vue"; //弹框form

import {
    saveyjQmpGyLlphb, //保存
    getLLPHBByFid, //详情
    getBaseDataitemDetailByItemCode, //数据字典
    getSpecificationDataList, //材料种类名称/规格
    getPurveyorDataList, //厂商

} from "@/api/request.js";

export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "statusNum",
        "currentRow",
        "fCompanyid",
        "fCompanyname",
        "modelType"
    ],
    data() {
        return {
            activeNames: ["0", "1"],
            fileList: [],
            isEdit: false,
            handNum: false, //闭合申请单
            firstNum: false, //一级审核
            secondNum: false, //二级审核
            visible: "",
            title: null,
            isLock: false,
            btnStatus: [],
            positionObj: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj, //基础信息
            formData: { //闭合 closeFormObj
                fId: "", //传fid表示修改 不传表示新增
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                recordfiles: [], //删除文件id集合
                fCompanyid: "", //标段id
                fCompanyname: "", //标段名称
                fProjectid: "", //项目id
                fProjectname: "", //项目名称
                fSgunitname: "", //施工单位名称
                fSgunitid: "", //施工单位id
                fHtnumber: "", //合同号
                fJlunitname: "", //监理单位名称
                fJlunitid: "", //监理单位id
                fLlphbnumber: "", //理论配合比报告编号 预新增提供
                fHnttype: "", //混凝土种类
                fHnttypeid: "",
                fQdlevel: "", //设计强度等级
                fSgsite: "", //混凝土施工部位
                fSgsiteList:[],
                fSgsiteid: "",
                fMixingmethod: "", //拌合及捣振方法
                fMixingmethodList: [], //拌合及捣振方法--页面展示
                fWatercement: "", //水胶比
                fDesignedslump: [0, 0], //设计塌落度 用~隔开
                fLlphbdates: "", //理论配合比报告日期
                fFileurl: "", //说明书及其他资料    预新增提供
                fReportid: "", //编制人id
                fReportname: "", //编制人姓名
                yjQmpGyLlphbrawmaterial: [], //理论配合比原材料信息
                fStatus: "", //状态
                fNote: ""
            },
            tableData: [],
            tableObj: [],
            materialObj: [], //材料集合
            specificationObj: [], //规格型号
            suppliernameObj: [], //生产厂商

            baseFormData: {},
            closeFormObj: formJs.closeFormObj, //闭合


            FauditFormObj: formJs.Faudit, //一级审核
            FauditFileurllist: [], //附件

            btnShow: true,
            currentDetail: null,


            fCompanynames: null,
            fCompanyids: null,
            modelTypes: null

        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        modelType: {
            handler(newVal) {
                if (newVal) {
                    this.modelTypes = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {


                    this.edit(newVal);
                }

            },
            immediate: true,
        },

        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentDetail = newVal;
                    // fStatus

                    if (this.statusNum != 1) {
                        this.getDetail({
                            fId: this.currentDetail.fId
                        }) // currentRow
                    }
                }
            },

            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {

                    this.fCompanyids = newVal

                }
            },
            immediate: true,
        },
        fCompanyname: {
            handler(newVal) {
                if (newVal) {
                    this.fCompanynames = newVal

                }
            },
            immediate: true,
        },

    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },

    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("monitorBack-userInfo"));
        this.btnShow = true
        this.getBaseDataitemDetailByItemCode('Hnttype', 1) //混凝土种类(Hnttype)
        this.getBaseDataitemDetailByItemCode('Qdlevel', 2) //设计强度等级(Qdlevel)
        this.getBaseDataitemDetailByItemCode('Sgsite', 3) //混凝土施工部位(Sgsite)
        this.handNum = true; //闭合申请单
        this.firstNum = true; //一级审核
        if (this.statusNum == 1) { //新增
            this.getDetail({
                fId: "",
                fCompanyid: this.userInfo.companyId
            })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = true;
            this.formObj[2].disabled = true;
            // this.formObj[3].disabled = true;
            this.formObj[11].uploadSelect = true
            this.isEdit = false
        } else if (this.statusNum == 2) { //编辑
            // this.getDetail({
            //     fId: this.currentDetail.fId
            // })
            this.formObj.map((item) => {
                item.disabled = false;
            });
            this.formObj[0].disabled = true;
            this.formObj[1].disabled = true;
            this.formObj[2].disabled = true;
            // this.formObj[3].disabled = true;
            this.formObj[11].uploadSelect = true
            this.isEdit = false

        } else if (this.statusNum == 7) { //查看
            this.btnShow = false

            // 闭合
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.isEdit = true

            this.formObj[11].uploadSelect = false
        }
        this.getHeight();
    },
    methods: {
        // 折叠面板
        changeCollapse(e) {
            console.log(this.activeNames, e);
        },
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },
        selectChange(data, a, b) {
            if (b == "fHnttype") { //混凝土种类
                this.formData.fHnttype = data.label;
                this.formData.fHnttypeid = data.value
            } else if (b == 'fSgsite') {//混凝土施工部位
                // this.formData.fSgsite = data.label
                // this.formData.fSgsiteid = data.value
                this.formData.fSgsiteList=data;
            } else if (b == 'fQdlevel') { //设计强度
                this.formData.fQdlevel = data.label
            } else if (b == "fMixingmethodList") { //拌和及振捣方法
                this.formData.fMixingmethodList = data; //拌和及振捣方法
            }
        },
        edit() {
            this.closeFormData = this.$options.data.call(this).closeFormData;
        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fileList.length; index++) {
                if (file.uid == this.fileList[index].uid) {
                    if (this.fileList[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fileList[index].atta.fId,
                        });
                    }
                    this.fileList.splice(index, 1);
                }
            }
        },

        //材料选择
        selectMaterial(row, name, index, item) { //存放位置选择
            this.formData.yjQmpGyLlphbrawmaterial[index].fMaterialname = item.fMaterialname;
            this.formData.yjQmpGyLlphbrawmaterial[index].fMaterialid = item.fMaterialid;

            this.tableObj[index].specifications = [];
            this.formData.yjQmpGyLlphbrawmaterial[index].fSpecification = [];

            // this.tableObj.map(item => {
            //     item['specifications'] = [];
            //     item.currentRow.map(i => {
            //         if (i.fMaterialname == name.fMaterialname) {
            //             item['specifications'].push(i)
            //         }
            //     })
            // })
            row.currentRow.map(v=>{
                if (v.fMaterialname==name.fMaterialname) {
                    this.tableObj[index].specifications.push(v);
                }
            })

            this.$forceUpdate()
            this.tableData = this.tableObj;
        },
        // 规格型号选择
        selectSpecification(row, name, index, item) { //存放位置选择
            this.formData.yjQmpGyLlphbrawmaterial[index].fSpecification = item.fVersion
        },
        // 厂商
        selectPurveyor(row, name, index, item) { //存放位置选择

            this.formData.yjQmpGyLlphbrawmaterial[index].fSuppliername = item.fMerchantname
            this.formData.yjQmpGyLlphbrawmaterial[index].fSupplierid = item.fId
        },
        // 数据字典
        getBaseDataitemDetailByItemCode(status, num) {
            getBaseDataitemDetailByItemCode({
                fItemcode: status
            }).then(res => {

                var data = res.data.baseDataitemdetailList
                var arr = []
                data.map(item => {
                    arr.push({
                        value: item.fItemdetailid,
                        label: item.fItemvalue
                    })
                })
                if (num == 1) { //混凝土种类
                    this.formObj[4].options = arr
                } else if (num == 2) { //设计强度等级
                    this.formObj[5].options = arr

                } else if (num == 3) { //混凝土施工部位
                    this.formObj[6].options = arr

                }
            })
        },
        // 去重
        fn3(tempArr) {
            let result = [];
            let obj = {};
            if (tempArr != null) {
                for (let i = 0; i < tempArr.length; i++) {
                    if (!obj[tempArr[i].fMaterialname]) {
                        result.push(tempArr[i]);
                        obj[tempArr[i].fMaterialname] = true;
                    }
                }
                return result;
            }

        },
        gets(data, id, index) {
            var obj = {
                page: 1,
                pageSize: 500,
                name: "", //材料种类/名称
                fVersion: "", //规格型号
                fId: id
            }
            this.tableObj[index]['specifications'] = []
            // this.loading = true;
            var that = this;
            getSpecificationDataList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        data.map(item => { //大表

                            if (res.data.pageInfo.list.length > 0) {
                                res.data.pageInfo.list.map(i => {
                                    if (item.fMaterialname == i.fMaterialname) {
                                        this.tableObj[index]['specifications'].push(i)
                                    }
                                })
                            }
                        })

                        that.$forceUpdate()
                        that.tableData = this.tableObj


                    } else {

                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
            // .catch((error) => {
            //     this.$message.error("列表数据 接口出错1", error.msg);
            //     this.loading = false;
            // });


        },
        // 材料名称
        getSpecificationDataLists(pageSize, id, index, status) {

            var obj = {
                page: 1,
                pageSize: 500,
                name: "", //材料种类/名称
                fVersion: "", //规格型号
                fId: id
            }
            // this.loading = true;
            var that = this;
            getSpecificationDataList(obj)
                .then((res) => {
                    if (res.errcode == 0) {

                        this.tableObj[index]['specificationData'] = [];
                        this.tableObj[index]['specificationData'] = this.fn3(res.data.pageInfo.list);
                        this.tableObj[index]['currentRow'] = res.data.pageInfo.list;
                        that.$forceUpdate()

                        if (status == 0) {
                            this.gets(this.tableObj, id, index)
                        } else {
                            that.tableData = this.tableObj
                        }

                        that.getPurveyorDataList(id, index)

                    } else {

                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
            // .catch((error) => {
            //     this.$message.error("列表数据 接口出错2", error.msg);
            //     this.loading = false;
            // });


        },
        // 厂商 getPurveyorDataList
        getPurveyorDataList(id, index) {
            var obj = {
                page: 1,
                pageSize: 500,
                merchantName: "", //生产厂家
                typ: "1", //"1"材料生产商，"2"材料供应商
                fVarietyid: id
            }
            // this.loading = true;
            // var that = this;
            getPurveyorDataList(obj)
                .then((res) => {

                    if (res.errcode == 0) {

                        this.tableObj[index]['PurveyorData'] = []
                        this.tableObj[index]['PurveyorData'] = res.data.pageInfo.list;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
            // .catch((error) => {
            //     this.$message.error("列表数据 接口出错3", error.msg);
            //     this.loading = false;
            // });
        },
        // 详情
        getDetail(obj) {
            var that = this
            getLLPHBByFid(obj).then((res) => {
                if (res.errcode == 0) {
                    var data = res.data.yjQmpGyLlphb

                    this.formData = data;

                    // 拌和及振捣方法
                    if (that.formData.fMixingmethod == "" || that.formData.fMixingmethod == null) {
                        that.formData.fMixingmethodList = [];
                    } else {
                        that.formData.fMixingmethodList = [];
                        var fMixingmethodArr = that.formData.fMixingmethod.split(",");
                        fMixingmethodArr.map((item) => {
                            that.formData.fMixingmethodList.push({
                                value: item,
                                label: item,
                            });
                        });
                    }
                    if (that.formData.fSgsite == "" || that.formData.fSgsite== null) {
                        that.formData.fSgsiteList = [];
                    } else {
                        that.formData.fSgsiteList = [];
                        var fSgsiteArr = data.fSgsite.split(",");
                        var fSgsiteidArr = data.fSgsiteid.split(",");
                        
                        fSgsiteArr.map(((item,index) => {
                            that.formData.fSgsiteList.push({
                                value: fSgsiteidArr[index],
                                label: item,
                            });
                        }));
                    }
                    // this.formData.fSgsite = data.label
                    // this.formData.fSgsiteid = data.value
                    // this.formData.fSgsiteList=data;
                    that.selected = [{
                        name: "fMixingmethodList",
                        selected: that.formData.fMixingmethod,
                    },{
                        name: "fSgsiteList",
                        selected: that.formData.fSgsite,
                    },];





                    var status = obj.fId == '' ? null : 0
                    data.yjQmpGyLlphbrawmaterial.map((item, index) => {
                        that.getSpecificationDataLists(data.yjQmpGyLlphbrawmaterial.length, item.fMaterialtypeid, index, status)

                    })
                    // this.tableData=data.yjQmpGyLlphbrawmaterial
                    this.tableObj = data.yjQmpGyLlphbrawmaterial

                    this.formData.fSgunitid = this.userInfo.companyId //标段id
                    this.formData.fSgunitname = this.userInfo.companyName //标段名称
                    this.formData.fJlunitid = this.userInfo.jlCompanyId //项目id
                    this.formData.fJlunitname = this.userInfo.jlCompanyName //项目名称
                    if (obj.fId == '') {
                        this.formData.fCompanyid = this.userInfo.companyId //标段id
                        this.formData.fCompanyname = this.userInfo.companyName //标段名称
                        this.formData.fDesignedslump = [0, 0]
                        this.formData.fLlphbdates = this.common.day(0)
                    } else {
                        this.formData.fDesignedslump = data.fDesignedslump.split(",")
                        this.fileList = res.data.fFileurllist
                    }




                }
            });
        },
        yj_saveForm() {
            // yjQmpGyLlphbrawmaterial
            if (
                this.formData.fMixingmethodList != "undefined" &&
                this.formData.fMixingmethodList.length > 0
            ) {
                var fMixingmethodArr = "";
                this.formData.fMixingmethodList.map((item) => {
                    fMixingmethodArr = fMixingmethodArr + "," + item.label;
                });

                this.formData.fMixingmethod = fMixingmethodArr.slice(
                    1,
                    fMixingmethodArr.length
                );
            } else {
                this.formData.fMixingmethod = "";
            }

            if (
                this.formData.fSgsiteList != "undefined" &&
                this.formData.fSgsiteList.length > 0
            ) {
                var fSgsiteArr = "";
                var fSgsiteidArr = "";

                this.formData.fSgsiteList.map((item) => {
                    fSgsiteArr = fSgsiteArr + "," + item.label;
                    fSgsiteidArr = fSgsiteidArr + "," + item.value;

                });

                this.formData.fSgsite = fSgsiteArr.slice(
                    1,
                    fSgsiteArr.length
                );
                this.formData.fSgsiteid = fSgsiteidArr.slice(
                    1,
                    fSgsiteidArr.length
                );
            } else {
                this.formData.fSgsite = "";
                this.formData.fSgsiteid = "";

            }

                         // this.formData.fSgsite = data.label
                    // this.formData.fSgsiteid = data.value
                    // this.formData.=data;
            this.userInfo = JSON.parse(sessionStorage.getItem("monitorBack-userInfo"));
            this.formData.userId = this.userInfo.userId; //当前登录人id
            this.formData.userName = this.userInfo.realName; //当前登录人名称
            this.formData.fReportid = this.userInfo.userId; //当前登录人id
            this.formData.fReportname = this.userInfo.realName; //当前登录人名称

            this.formData.fProjectid = this.userInfo.projectId //项目id
            this.formData.fProjectname = this.userInfo.projectName //项目名称

            if (this.$refs.publicForm.submitForm()) {
                this.formData.fDesignedslump = this.formData.fDesignedslump.join(",")

                this.saveForm(this.formData, 0);
            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(obj) {
            // var url;

            // if (status == 0) {
            //     url = saveOutsourcednon //闭合
            // } else if (status == 1) {
            //     url = checkOutsourcednon1 //一级审核
            // } else if (status == 2) {
            //     url = checkOutsourcednon2 //二级审核
            // }
            this.isLock = true
            saveyjQmpGyLlphb(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                        this.isLock = false
                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });
        },
    },
};